<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Web Sql</title>
  <link rel="stylesheet" href="./demo.css">
</head>

<body>
  <div id="app">
    <div class="btn" @click="saveWebSql">webSql存储</div>
    <div class="btn" @click="getWebSql">webSql获取</div>
    <div class="btn" @click="deleteWebSql">webSql删除</div>
  </div>
  <script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.min.js"></script>
  <script>
    let app = new Vue({
      el: '#app',
      data: {
        myWebSql: '',
        config: {
          name: 'my_plan',
          version: '1.0',
          desc: 'manage my plans',
          size: 2 * 1024 * 1024
        }
      },
      created() {
        this.initWebSql()
      },
      methods: {
        initWebSql() {
          this.myWebSql = window.openDatabase(this.config.name, this.config.version, this.config.desc, this.config.size)
          let sql = 'create table if not exists t_plan(title, content, target_time, create_time)'
          this.myWebSql.transaction((tx) => {　　
            tx.executeSql(sql, null, (tx, rs) => {
              console.log('创建表')
            })
          })
        },
        saveWebSql() {
          let sql =
            'insert into t_plan (title, content, target_time, create_time) VALUES ("测试","sdfasdfasfasf",2018,2018)'
          this.myWebSql.transaction((tx) => {　　
            tx.executeSql(sql, null, (tx, rs) => {
              let effectRow = rs.rowsAffected
              console.log(`影响记录条数: ${effectRow}`)
            })
          })
        },
        getWebSql() {
          let sql = 'select * from t_plan where title="测试"'
          this.myWebSql.transaction((tx) => {　　
            tx.executeSql(sql, null, (tx, rs) => {
              console.log('获取成功', rs)
            })
          })
        },
        deleteWebSql() {
          let sql = 'delete  from t_plan where title="测试"'
          this.myWebSql.transaction((tx) => {　　
            tx.executeSql(sql, null, (tx, rs) => {
              console.log('删除成功', rs)
            })
          })
        }
      }
    })
  </script>
</body>

</html>